<template>
  <div>
    <breadcrumb>
      <span slot="first">首页</span>
      <span slot="second">商品管理</span>
      <span slot="third">商品列表</span>
    </breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input @clear="getGoodsList" clearable v-model="queryInfo.query" placeholder="请输入内容">
            <el-button @click="getGoodsList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addButton">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->

      <el-table :data="goodList" border style="width: 100%">
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="650">
        </el-table-column>
        <el-table-column prop="goods_price" label="价格(元)" width="100">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editById(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { request } from '@/network/request.js'
import Breadcrumb from '@/components/common/Breadcrumb.vue'
import { formatDate } from '@/components/common/utils/formatDate.js'
export default {
  name: "List",
  components: { Breadcrumb },
  data() {
    return {
      // 商品列表
      goodList: [],
      // 查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      // 当前商品数量
      total: 0,
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await request({
        url: 'goods',
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return
      this.total = res.data.total
      // 处理时间戳
      res.data.goods.forEach(item => {
        let date = new Date(item.add_time * 1000)
        item.add_time = formatDate(date, "yyyy-MM-dd hh:mm:ss")
      })
      this.goodList = res.data.goods

    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 根据id删除商品
    async removeById(id) {
      const flag = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (flag !== 'confirm') return this.$message.info("取消删除")
      // 发送网络请求
      const { data: res } = await request({
        method: "delete",
        url: `goods/${id}`
      })
      if (res.meta.status !== 200) return
      this.$message.success("删除成功")
      this.getGoodsList()
    },
    // 添加商品
    addButton() {
      this.$router.push("/goods/add")
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>

<style lang='less' scoped>
.el-pagination {
  margin: 10px;
}
</style>